/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：InventoryScreen.java <br>
 * 创建时间：2013-12-23 下午7:04:35 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui;

import com.orhanobut.logger.Logger;

import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.MainCharTile;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.MapTile;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.battle.ViewScreen;
import lostland.gmud.exv2.battle.proc.DummyStatus;
import lostland.gmud.exv2.battle.proc.FreeStatus;
import lostland.gmud.exv2.battle.proc.PreAttackStatus;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.data.Item;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.data.Skill;
import lostland.gmud.exv2.ui.core.DoubleScrollScreen;
import lostland.gmud.exv2.ui.core.NewButton;

/**
 * 类名：InventoryScreen <br>
 * 说明：物品栏的操作（使用、装备、丢弃等）
 *
 * @author 12548
 */
public class InventoryScreen extends DoubleScrollScreen {

	Item sticking[][];

	CScreen ts;
	AutoWindow bottomWindow;


	public InventoryScreen(boolean b, CScreen ts) {
		super(30, 30, 36, 222, 6);
		setInbattle(b);
		gets();
		v1 = new LeftScrollView(s1, x, y, w1, max);
		re();
		this.ts = ts;
		bottomWindow = new AutoWindow(x, y + max * 12, w1 + w2 - 1, 54, "");
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#gets()
	 */
	@Override
	public void gets() {
		sticking = new Item[10][Gmud.mc.inventory.size() + 1];

		if (inBattle()) {
			s1 = new String[]{"药物", "投掷"};//1 and 5
			if (s2 == null) {
				s2 = new String[2][];
			}

			int k = 0;
			for (Item j : Gmud.mc.inventory)
				if (Item.notNull(j) && j.getKind() == 1) {
					k++;
				}
			s2[0] = new String[k];

			k = 0;
			for (Item j : Gmud.mc.inventory) {
				if (Item.notNull(j) && j.getKind() == 1) {
					String suffix = "×" + Gmud.mc.get_pile(j);
					if (j.getTemplate().getPile() < 2) {
						suffix = "";
					}
					s2[0][k] = j.getName() + suffix;
					sticking[0][k] = j;
					k++;
				}
			}

			k = 0;
			for (Item j : Gmud.mc.inventory)
				if (Item.notNull(j) && j.getKind() == 5) {
					k++;
				}
			s2[1] = new String[k];

			k = 0;
			for (Item j : Gmud.mc.inventory) {
				if (Item.notNull(j) && j.getKind() == 5) {
					int maxc = Gmud.mc.get_pile(j);
					String suffix = "×" + (Gmud.mc.equips(j) ? maxc - 1 : maxc);
					if (j.getTemplate().getPile() < 2) {
						suffix = "";
					}
					s2[1][k] = j.getName() + suffix;
					sticking[1][k] = j;
					k++;
				}
			}
		} else {
			s1 = new String[]{"食物", "药物", "武器", "装备", "其他", "暗器", "丢弃"};
			if (s2 == null) {
				s2 = new String[7][];
			}

			int i, k;
			for (i = 0; i < 6; i++) {
				k = 0;
				for (Item j : Gmud.mc.inventory)
					if (Item.notNull(j) && j.getKind() == i) {
						k++;
					}

				s2[i] = new String[k];

				k = 0;
				for (Item j : Gmud.mc.inventory) {
					if (Item.notNull(j) && j.getKind() == i) {
						String suffix = "×" + Gmud.mc.get_pile(j);
						if (j.getTemplate().getPile() < 2) {
							suffix = "";
						}
						s2[i][k] = j.getName() + suffix;
						sticking[i][k] = j;
						k++;
					}
				}
			}
			i = 6;

			k = 0;
			for (Item j : Gmud.mc.inventory)
				if (Item.notNull(j) && j.canDrop()) {
					k++;
				}

			s2[i] = new String[k];

			k = 0;
			for (Item j : Gmud.mc.inventory) {
				if (Item.notNull(j) && j.canDrop()) {
					String suffix = "×" + Gmud.mc.get_pile(j);
					if (j.getTemplate().getPile() < 2) {
						suffix = "";
					}
					s2[i][k] = j.getName() + suffix;
					sticking[i][k] = j;
					k++;
				}
			}
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#onClick(int)
	 */
	@Override
	public void onClick(int c) {
		int d = v1.cursor;

		final Item item = sticking[d][c];
//		if(Gmud.mc.equips(19)
//				&& Gmud.ms.map.id == 2
//				&& MainCharTile.frontAbsX() == 8
//				&& MainCharTile.frontAbsY() == 3)
//		{
//			if(item.getId() == 76){
//				if(Gmud.mc.isHungry()){
//					game.setScreen(new TalkingScreen(game,"你已经快不行了了，这样下去会有生命危险！", false));
//					return;
//				}
//				game.setScreen(new TalkingScreen(game,"你坐在鱼篓里用麻绳索降到了井下。",false));
//				Gmud.ms.jumpTo(47, 6, 14, Direction.RIGHT);
//			}
//			else if(item.getId() == 77){
//				game.setScreen(new TalkingScreen(game,"活着真没意思，真想绑个石头跳井自杀。",false));
//				TalkingScreen.ts = new YesNoScreen(game,"警告！你一旦在这里自杀，你所有周目的存档就都清空了，请斟酌而定！"){
//					@Override
//					protected void onYes() {
//						game.oldmc.clear();
//						new SavingScreen(game).save();
//						IFileIO f = game.getFileIO();
//						while(!f.getPreferences().edit().putBoolean("newgame", true).commit())
//							;
//						game.setScreen(new BlankScreen(game));
//					}
//
//					@Override
//					protected void onNo() {
//						game.popToRootScreen();
//					}
//				};
//			}
//			return;
//		}
//		else
//			if(item.getId() == Room.super_toys[0]){
//			// 十玩具10：女娲石
//			if(Gmud.ms.map.getWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY()) == GmudMap.MP_NPC){
//				Integer npc_id = Gmud.ms.map.getEvent(MainCharTile.frontAbsX(), MainCharTile.frontAbsY());
//				if(game.data.death_note.contains(npc_id)){
//					Gmud.mc.drop(item, 1);
//					if(Gmud.rand.nextBoolean()){
//						game.data.death_note.remove(npc_id);
//						game.setScreen(new TalkingScreen(game,"太好了！你成功地用女娲石复活了 " + Gmud.getNpc(npc_id).name + " ！", false));
//						Gmud.mc.fame = Gmud.mc.fame < 128 ? 127 : 129;
//						Gmud.mc.potential /= 2;
//					}
//				}
//			}
//			return;
//		}
//		else if(item.getId() == Room.super_toys[4]){
//			// 十玩具4：炼妖壶
//			int front = Gmud.ms.map.getWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY());
//			if(front == GmudMap.MP_NPC){
//				Integer npc_id = Gmud.ms.map.getEvent(MainCharTile.frontAbsX(), MainCharTile.frontAbsY());
//				if(!game.data.death_note.contains(npc_id)){
//					game.data.vanish_note.add(npc_id);
//					Gmud.ms.map.setEvent(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), -1);
//					Gmud.ms.map.setWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), GmudMap.MP_WALKABLE);
//					game.setScreen(new TalkingScreen(game,"你成功地将 " + Gmud.getNpc(npc_id).name + " 吸入了炼妖壶！", false));
//					Gmud.mc.exp -= 5000;
//				}
//			} else
//				if(front == GmudMap.MP_WALKABLE){
//				final ArrayList<String> fs = new ArrayList<String>();
//				for(Npc npc : Gmud.npc){
//					if(game.data.vanish_note.contains(npc.id))
//						fs.add(npc.name);
//				}
//
//				game.setScreen(new GeneralMenuScreen(game, fs, "请选择一个想要从炼妖壶里放出来的NPC：") {
//					@Override
//					public void onCancel() {
//						game.setParentScreen();
//					}
//					@Override
//					public void onClick(int i) {
//						Integer npcid = -1;
//						for(Npc npc : Gmud.npc){
//							if(npc.name.equals(fs.get(i)))
//								npcid = npc.id;
//						}
//						Gmud.ms.map.setWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), GmudMap.MP_NPC);
//						Gmud.ms.map.setEvent(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), npcid);
//						game.data.vanish_note.remove(npcid);
//						game.setScreen(new TalkingScreen(game,"你将 " + fs.get(i) + " 从炼妖壶里放了出来！", false));
//					}});
//			}
//			return;
//		}
//		else if(item.getId() == Room.super_toys[3] && d != 5){
//			// 十玩具3：盘古斧
//			if(Gmud.ms.map.getWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY()) != GmudMap.MP_WALKABLE){
//				game.setScreen(new YesNoScreen(game,"你确定要使用盘古斧砍开面前的东东吗？"){
//					@Override
//					protected void onYes() {
//						Gmud.ms.map.setEvent(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), -1);
//						Gmud.ms.map.setWalkable(MainCharTile.frontAbsX(), MainCharTile.frontAbsY(), GmudMap.MP_WALKABLE);
//						game.setScreen(new TalkingScreen(game, "望望头上天外天，走走脚下一马平川！", false));
//					}
//					@Override
//					protected void onNo() {
//						Gmud.mc.equip(item);
//						game.setScreen(new TalkingScreen(game, "你装备了盘古斧。", false));
//					}
//				});
//			}
//			else
//				Gmud.mc.equip(item);
//			return;
//		}

		if (inBattle()) {
			if (d == 1) {
				d = 5;
			} else if (d == 0) {
				d = 1;
			}
		}
		switch (d) {
			case 0: //食物
				int a = item.getA3();
				int bb = item.getA4();
				if (Gmud.mc.food < Gmud.mc.getFoodmax()) {
					Gmud.mc.drop(item, 1);
					Gmud.mc.food += a;
					Gmud.mc.drink += bb;
				}
				break;
			case 1: //药物
				switch (item.getSubkind()) {
					case 0:
						if(Gmud.mc.getHp() < Gmud.mc.getAttr(Room.ATTR_MAXHP)) {
							Gmud.mc.drop(item, 1);
							Gmud.mc.cure((int) (Gmud.mc.getAttr(Room.ATTR_MAXHP) * ((item.getA3() + 1) * 0.25)));
						}
						break;
					case 1: //海外咸蛋
						Gmud.mc.drop(item, 1);
						Gmud.mc.maxfp_bouns += item.getA3();
						break;
					case 2: //绞股蓝（加内力，解麻痹）
						if (Gmud.mc.get_pile(item) <= 10) {
							Gmud.mc.drop(item, 1);
							int addfp = Math.max(item.getA3(), (Gmud.mc.getAttr(Room.ATTR_MAXFP) - Gmud.mc.getFp()) / 10);
							addfp = Math.min(Gmud.mc.getAttr(Room.ATTR_MAXFP) - Gmud.mc.getFp(), addfp);
							Gmud.mc.setFp(Gmud.mc.getFp() + addfp);
							if (Gmud.mc.getDz() > 0) {
								Gmud.mc.setDz(Gmud.mc.getDz() - 1);
							}

							if (inBattle()) {
								new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
								new DummyStatus().pushToBattle();
								new ViewScreen("你吃了绞股蓝，缓解了麻痹症状和部分已损失内力。你还须呆若" + Gmud.mc.getDz() + "次木鸡").pushToGame();
							}
						} else {
							Gmud.mc.drop(item, 1);
							Gmud.mc.setFp(Gmud.mc.getFp() - item.getA3());
							if (Gmud.mc.getFp() <= 0) {
								Gmud.mc.setFp(0);
								Gmud.mc.maxfp--;
							}
						}
						break;
					case 3: //自动人偶
						if (inBattle()) {
							BattleScreen.getInstance().trust = true;
							Gmud.mc.drop(item, 1);
							new ViewScreen("你召唤出了夜夜替你作战。").pushToGame();
						}
						break;
				}
				if (inBattle()) {
					onCancel();
				}
				break;
			case 2:
			case 3:
				Gmud.mc.equip(item);
				break;
			case 4:
				if (item.getTriggerOnUse() >= 0) {
					Logger.i("Calling Trigger: " + item.getTriggerOnUse());
					getGame().callTriggerEvent(item.getTriggerOnUse(), false, 0, 0, 0f, "", 0);
				}
//			if(item.getKind() == 4)
//			{
//				if(item.getId() == 81 && Gmud.mc.sex != 2)
//				{
//					game.setScreen(new YesNoScreen(game,"修炼本秘籍需要超乎常人的勇气和毅力，你真的要修炼吗？"){
//
//						@Override
//						protected void onYes() {
//							boolean haveknife = false;
//							for(ItemTemplate i : Gmud.wp){
//								if(i.getKind() == 2 && (i.getSubkind() == 1 || i.getSubkind() == 6) && Gmud.mc.have(i.getId())){
//									haveknife = true;
//									break;
//								}
//							}
//							if(!haveknife)
//								game.setScreen(new NotificationScreen(game,this,"工欲自其宫，必先备其刀。还是先去准备一把利器吧！"));
//							else
//								game.setScreen(new YesNoScreen(game,"最后问你一句，不后悔吗？"){
//									@Override
//									protected void onYes() {
//										Gmud.mc.sex = 2;
//										game.popToRootScreen();
//									}
//									@Override
//									protected void onNo() {
//										game.setParentScreen();
//									}
//								});
//						}
//
//						@Override
//						protected void onNo() {
//							game.setParentScreen();
//						}
//					});
//				}
//				else
//				{
//					if(Item.getitmnpc(item.getId()) == 0)
//						;
//					else if(Gmud.mc.skills[Skill.KIND_ZHISHI] <= 0)
//						game.setScreen(new NotificationScreen(game,this,"你还是个文盲！"));
//					else if(Gmud.mc.haveTalent(2049))
//						game.setScreen(new TradeScreen(game,Item.getitmnpc(item.getId()), 102));
//					else if(Gmud.mc.faction != 7 && Gmud.mc.faction != 0 && item.getId() < 82)
//						game.setScreen(new NotificationScreen(game,this,"还是先学好本门武功吧"));
//					else
//					{
//						if(item.getId() < 82)
//							Gmud.mc.faction = 7;
//						game.setScreen(new TradeScreen(game,Item.getitmnpc(item.getId()), 102));
//					}
//				}
//			}
				break;
			case 5:

				if (inBattle()) {

					Npc bdp = Gmud.getNpc(BattleScreen.getInstance().enemyid);
					int sklid = item.getSubkind();
					if (Gmud.skill[sklid].pos != 5) {
						sklid = Gmud.mc.getEquipedSkill(5).id;
					}
					Skill skill = Gmud.skill[sklid];
					Gmud.mc.drop(item);
					PreAttackStatus st = new PreAttackStatus(Gmud.mc, bdp, skill.chooseAGesture());
					st.anqi = item;
					st.pushToBattle();
					getGame().popScreen();
//				getGame().popScreen();
//
//				if(BattleScreen.getInstance().qkdny1 && junk.getKind() == 2){
//					bdp.equip(item);
//					BattleScreen.getInstance().qkdny1 = false;
//					getGame().replaceCurrentScreen(new ViewScreen("你双手轻轻牵引，" + junk.getName() + "像长了翅膀一样，轻巧地飞到了" + bdp.name + "的手里！"));
//					break;
//				}
//
//				int damage = Math.min(5000, junk.getCost()) / Math.max(13, 18-bdp.getStr()+bdp.str) + junk.getA3();
//				//公式：物品价格(最多1W) / 18-膂力增量(最少13) + 物品的a3值(武器为攻击力，防具为防御力)
//				//理论初始最大伤害：5000 / 13 + 99(绣花针) = 484
//				damage *= (int)Math.pow(1.5, junk.getA6() - 4);
//				//加成后最大伤害：484*3.375=1632
//				damage -= bdp.getAttr(Room.ATTR_DEF) * 10;
//				//考虑对方防御值，若防御为20，则可减免200点伤害
//				if(damage < -20) {
//					damage = 0;
//				}
//				else {
//					Gmud.Format(damage, 20, -1);
//				}
//				if(damage > 600 && !Gmud.mc.haveTalent(2051)) {
//					damage = 450;
//				}
//				//高伤害限制 天赋 暗箭难防
//				Gmud.mc.drop(item, 1);
//				String txt;
//				if(Gmud.mc.getDz() > 1 || Gmud.mc.fp < damage/3) {
//					txt = "你挪动僵硬的手腕入怀中掏摸，却不料手一滑，将" + item.getName() + "掉在了地上！";
//				}
//				else{
//					txt = "你卖了一个破绽，在怀中掏摸两下，将" + item.getName() + "向" + bdp.name + "狠狠地丢了过去！";
//					if(damage > 400){
//						if(Math.random() * 3 > 1.0) {
//							damage /= 2;
//						}
//					}
//					Gmud.mc.fp -= damage/3;
//					bdp.dmg(damage, 0);
//				}
//				getGame().replaceCurrentScreen(new ViewScreen(txt));
				} else {
//				Gmud.mc.drop(item, 1);
				}
				break;
			case 6:
				if (Gmud.mc.only(item)) {
					break;
				}
				Gmud.mc.drop(item, 1);
		}

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#drawbg()
	 */
	@Override
	public void drawbg() {
		if (inBattle()) {
			BattleScreen.getInstance().present(0);
		} else {
			MapScreen.getInstance().getMap().draw(MapTile.getInstance(), MapScreen.X, MapScreen.Y);
			MainCharTile.getInstance().drawInMap(MainCharTile.getInstance().currentDirection, MainCharTile.getInstance().currentStep, MainCharTile.getInstance().X, MainCharTile.getInstance().Y);
		}
		bottomWindow.draw();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#cancel()
	 */
	@Override
	public void cancel() {
//		game.setScreen(ts);
		getGame().popScreen();
		if (inBattle()) {
			new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {
		super.onButtonClick(b);
		if (bottomWindow != null)
			if (layer == 1) {
				bottomWindow.text = sticking[v1.cursor][v2.cursor].getDes();
			} else {
				bottomWindow.text = "";
			}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DoubleScrollScreen#checkin()
	 */
	@Override
	public void checkin() {
		if (v1.cursor == 5) {
			for (int i = 0; i < v2.buttons.length; i++) {
				v2.buttons[i].setChecked(false);
			}
		} else {
			for (int i = 0; i < v2.buttons.length; i++) {
				boolean check = Gmud.mc.equips(sticking[v1.cursor][i]);

				if (check) {
					for (int j = i - 1; j >= 0; j--)
						if (sticking[v1.cursor][j].equals(sticking[v1.cursor][i])) {
							check = false;
						}
				}

				v2.buttons[i].setChecked(check);
			}
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {
		super.update(deltaTime);


		if (layer == 1)
			if (sticking[v1.cursor][v2.cursor] != null)
				if (sticking[v1.cursor][v2.cursor].getId() == 91) {
					String string = "一本日历，上面写着：" + Gmud.getCalString();
					int yc = getGame().data.yellowcal;

					if (yc == 0) {
						string += "，诸事不宜";
					} else {
						if ((yc & 1) != 0) {
							string += "，宜寻人寻物";
						} else if ((yc & 2) != 0) {
							string += "，宜行侠仗义";
						} else if ((yc & 4) != 0) {
							string += "，宜杀人放火";
						} else if ((yc & 8) != 0) {
							string += "，宜为非作歹";
						}
					}

					string += "。";

					bottomWindow.text = string;
				}
	}

	protected boolean inBattle() {
		return __inventory_inbattle;
	}

	protected void setInbattle(boolean __inventory_inbattle) {
		this.__inventory_inbattle = __inventory_inbattle;
	}
}
